# Maintainer: Daurnimator <daurnimator@archlinux.org>
# Contributor: Michael Beaumont <mjboamail@gmail.com>

pkgname=sops
pkgver=3.7.3
pkgrel=1
pkgdesc='Editor of encrypted files that supports YAML, JSON and BINARY formats'
arch=('loong64' 'x86_64')
url='https://github.com/mozilla/sops'
license=('MPL2')
depends=('glibc')
makedepends=('go')
options=('!lto')
source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz"
        'dependency-update.patch'
        'bash_autocomplete'
        'zsh_autocomplete')
sha256sums=('0e563f0c01c011ba52dd38602ac3ab0d4378f01edfa83063a00102587410ac38'
            '40ec325ce884579c611075cd3b9426b5fe820760049b40d0b6c6d441b99bae6d'
            '761c67ffd48431e60ea5dc48e5656b4480eca7534d4b715eb65bd75680f5e975'
            'd98d34aeaa7b0f98220bcb21fc9f2bb58efaed45478a7229eb58e5795b57b5a7')

prepare() {
  cd "${pkgname}-${pkgver}"

  # FS#70588
  patch -p1 -i ../dependency-update.patch

  go mod download
}

build() {
  cd "${pkgname}-${pkgver}"

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOPROXY=off
  go build \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode=external -extldflags \"${LDFLAGS}\"" \
    ./cmd/sops
}

check() {
  cd "${pkgname}-${pkgver}"

  go test
}

package() {
  install -Dm644 bash_autocomplete "${pkgdir}/usr/share/bash-completion/completions/sops"
  install -Dm644 zsh_autocomplete "$pkgdir/usr/share/zsh/site-functions/_sops"

  cd "${pkgname}-${pkgver}"

  install -Dm755 sops "${pkgdir}/usr/bin/sops"
  install -Dm644 README.rst "${pkgdir}/usr/share/doc/${pkgname}/README.rst"
}
